package com.lianzhihui.romeex.ui.ecological;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.adapter.IntegralTransferRecordAdapter;
import com.lianzhihui.romeex.bean.EcologicalBean;
import com.lianzhihui.romeex.bean.MnnerTransferListBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.NodebuyPresnterImp;
import com.lianzhihui.romeex.mvp.view.NodeBuyView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.IntentUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Description: 转账记录
 * Author:
 * CreateDate: 2020/11/6 10:20 AM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class IntegralTransferRecordActivity extends BaseActivity implements NodeBuyView, OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.refresher)
    SmartRefreshLayout refresher;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private static final int pageSize = 20;
    private IntegralTransferRecordAdapter adapter;
    private NodebuyPresnterImp nodebuyPresnterImp;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zz_record_layout;
    }

    @Override
    protected void initView() {
        setTitle("转账记录");
        refresher.setOnRefreshLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        nodebuyPresnterImp = new NodebuyPresnterImp(this, this);
        initRecyclerView();
        refresher.autoRefresh();
    }

    private void initRecyclerView() {
        if (adapter == null) {
            adapter = new IntegralTransferRecordAdapter(new ArrayList<>());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(adapter);
            adapter.setEmptyView(getEmptyView());
        }
        adapter.setOnItemClickListener(this);
    }


    @Override
    public void setFailure() {
        refresher.finishLoadMore();
        refresher.finishRefresh();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        getData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        getData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        MnnerTransferListBean.ContentBean item = (MnnerTransferListBean.ContentBean) adapter.getItem(position);
        IntentUtils.JumpToHaveObj(IntegralTransferDetailsActivity.class, AppConstants.RECORD_DETAILS_KEY, item);
    }

    private void getData() {
        nodebuyPresnterImp.requestMinnerTransferList(pageNum, pageSize, "2");
    }

    @Override
    public void responseMinnerTransferListSuccess(MnnerTransferListBean b) {
        if (b != null) {
            if (pageNum == 1) {
                adapter.setNewData(b.getContent());
            } else {
                adapter.addData(b.getContent());
            }
        }
        pageNum++;
        refresher.finishRefresh();
        refresher.finishLoadMore();
    }

    @Override
    public void responseMinnerMyBalanceSuccess(EcologicalBean b) {

    }

    @Override
    public void responseMinnerBuyNodeSuccess() {

    }
}
